const db = require("../config/config");
const jsonWebToken = require("jsonwebtoken");
const SECRET_KEY = "kite1874";
module.exports = {
  getAllData(req, res) {
    db.dbMysql(`SELECT * FROM purchaseView`, null, function (err, data) {
      console.log(data);
      if (data) {
        data.forEach((item) => {
          item.key = item.purchaseId;
        });
        const data1 = data; //抢购数据
        res.json({
          code: 200,
          msg: "查询成功",
          data: data1,
        });
      }
    });
  },
  getAppointData(req, res) {
    console.log(req.body);
    db.dbMysql(
      `SELECT * FROM purchaseView WHERE purchaseTitle like "%${req.body.purchaseTitle}%" AND productGrounding=${req.body.productGrounding}`,
      null,
      function (err, data) {
        if (!err) {
          console.log(data);
          console.log(data.length);
          if (data.length === 0) {
            res.json({
              code: 200,
              msg: "查询无数据",
            });
          } else {
            data.forEach((item) => {
              item.key = item.deductionId;
            });
            const data1 = data;
            res.json({
              code: 200,
              msg: "查询成功",
              data: data1,
            });
          }
        } else {
          res.json({
            code: 500,
            msg: "获取商品失败，请稍后重试",
          });
        }
      }
    );
  },
  isProductId(req, res) {
    console.log(req.body);
    db.dbMysql(
      `SELECT productID FROM product WHERE productID=${req.body.ProductId}`,
      null,
      function (err, data) {
        if (data.length === 0) {
          res.json({
            code: 200,
            msg: "查询无数据",
          });
        }
      }
    );
  },
  addData(req, res) {
    console.log(req.body);
    const reqData = req.body;
    db.dbMysql(
      `INSERT INTO purchase (purchaseId,purchaseTitle,purchaseDetail,purchaseType,purchaseProductId,startTime,endTime,productGrounding) VALUES (?,?,?,?,?,?,?,?)`,
      [
        null,
        reqData.purchaseTitle,
        reqData.purchaseDetail,
        reqData.purchaseType,
        reqData.purchaseProductId,
        reqData.startTime,
        reqData.endTime,
        reqData.productGrounding,
      ],
      function (err, data) {
        if (data) {
          res.json({
            code: 200,
            data: data,
            msg: "添加成功",
          });
        } else {
          res.send({
            code: 500,
            msg: "服务器错误",
          });
        }
      }
    );
  },
  delData(req, res) {
    console.log(req.body);
    db.dbMysql(
      `DELETE FROM  purchase WHERE purchaseId = ${req.body.id}`,
      null,
      function (err, data) {
        if (data) {
          res.json({
            code: 200,
            msg: "删除成功",
          });
        } else {
          res.send({
            code: 500,
            msg: "删除失败",
          });
        }
      }
    );
  },
  editData(req, res) {
    console.log(req.body);
    const reqData = req.body;
    db.dbMysql(
      `UPDATE purchase SET purchaseTitle='${reqData.purchaseTitle}',purchaseDetail='${reqData.purchaseDetail}',purchaseType='${reqData.purchaseType}',purchaseProductId='${reqData.purchaseProductId}',startTime='${reqData.startTime}',endTime='${reqData.endTime}',productGrounding=${reqData.productGrounding} WHERE purchaseId = ${reqData.purchaseId}`,
      null,
      function (err, data) {
        console.log(err);
        if (!err) {
          res.json({
            code: 200,
            data: data,
            msg: "修改成功",
          });
        } else {
          res.send({
            code: 500,
            msg: "服务器错误",
          });
        }
      }
    );
  },
};
